# -*- coding;utf-8 -*-
"""
File name : tasks.py.PY
Program IDE : PyCharm
Create file time: 2023/10/18 8:25
File Create By Author : 朱清闯
"""
from celery_tasks.main import app
from account.models import CustomerBankPointChangeLog, CompanySupplyBankPointLog
from account.serializers import CustomerBankPointChangeLogModelSerializer
from time import sleep


# 异步任务：创建个人库币交易记录，即接收者收入、发起者、交易库币的金额和交易类型（如提现、充值、下载等等）记录，创建用反序列化实现，注意pk
# operator发送者pk、point_amount消费金额、remarks备注、transaction_type交易类型（如下载、充值、提现等）、receiver接收者pk
# bank_point_log_data = {
#     'operator': user_company.pk,
#     'point_amount': server_obj.amount,
#     'remarks': '全额提现',
#     'transaction_type': 2,
#     'receiver': server_obj.user.pk
# }
@app.task(name='create_customer_bank_point_log_change')
def create_customer_bank_point_log_change(operator, receiver, point_amount, transaction_type=1, remarks='下载'):
    data = {
        'operator': operator,
        'point_amount': point_amount,
        'remarks': remarks,
        'transaction_type': transaction_type,
        'receiver': receiver}
    # print('要异步创建bank_point记录', data)
    serializer = CustomerBankPointChangeLogModelSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
    else:
        print('异步任务创建库币交易记录失败error', serializer.errors)


@app.task(name='create_company_supply_bank_point_log')
def create_company_supply_bank_point_log(operator, point_amount, remarks):
    CompanySupplyBankPointLog.objects.create(operator=operator,
                                             point_amount=point_amount,
                                             remarks=remarks)


@app.task
def send(a, b):
    # 具体要执行的任务
    print("开始")
    print(a, b)
    sleep(4)
    print("结束")
    return 1
